package com.funny.springcloud.controller;

import com.funny.springcloud.feign.PaymentFeignService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@DefaultProperties(defaultFallback="paymentGlobalFallback")
@Slf4j
@RestController
public class OrderHystrixController {

    @Resource
    private PaymentFeignService paymentFeignService;

    @GetMapping("/consumer/payment/hystrix/ok/{id}")
    public String paymentInfoOK(@PathVariable("id") Integer id) {
        return paymentFeignService.paymentInfoOK(id);
    }

    /*@HystrixCommand(fallbackMethod = "paymentInfoTimeoutFallback",
            commandProperties = {
                    @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1500")
            })*/
    @HystrixCommand
    @GetMapping("/consumer/payment/hystrix/timeout/{id}")
    String paymentInfoTimeout(@PathVariable("id") Integer id) {
        // int t = 10 / 0;
        return paymentFeignService.paymentInfoTimeout(id);
    }

    public String paymentInfoTimeoutFallback(@PathVariable("id") Integer id) {
        return "消费者80,对方支付系统繁忙，请10秒后再试 o(╥﹏╥)o";
    }

    // 全局fallback方法
    public String paymentGlobalFallback() {
        return "Global异常处理信息，请稍后再试 o(╥﹏╥)o";
    }
}
